package leetcode.Y2024M6.study;

import leetcode.Y2024M6.TreeNode;
import org.junit.Test;

/**
 * 解答错误也是值得一看的
 */
public class S112 {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if (root == null) return false;
        // 左右节点为空才是叶子节点
        if (root.left == null && root.right == null) {
            return root.val == targetSum;
        }
        return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val);

    }

    @Test
    public void test() {
        int[] root = {5, 4, 8, 11, -1, 13, 4, 7, 2, -1, -1, -1, 1};
        TreeNode tree = TreeNode.createTree(root);
        System.out.println(hasPathSum(tree, 22));
    }

    @Test
    public void test1() {
        int[] root = {1, 2, 3};
        TreeNode tree = TreeNode.createTree(root);
        System.out.println(hasPathSum(tree, 5));
    }

    @Test
    public void test3() {
        int[] root = {1, 2};
        TreeNode tree = TreeNode.createTree(root);
        System.out.println(hasPathSum(tree, 0));
    }
}
